package com.etime.controller;

import com.etime.pojo.Station;
import com.etime.pojo.User;
import com.etime.service.StationService;
import com.etime.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StationService stationService;
    @RequestMapping("getAllUser")
    public ModelAndView getAllStudent(ModelMap modelMap) {
        List<User> list = userService.getAllUser();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("main");
        modelMap.addAttribute("userList",list);
        return modelAndView;
    }
    @RequestMapping("login")
    public String login(User user, HttpSession session){
        boolean result= userService.login(user,session);
        if (result) {
            return "redirect:../back/index.jsp";
        }else {
            return "redirect:../back/login.jsp";
        }
    }
    @RequestMapping("logout")
    public String logout(HttpSession session){
//        session.removeAttribute("loginUser");
        session.invalidate();
        return "redirect:../back/login.jsp";
    }
    @RequestMapping("getUserByPage")
    public ModelAndView getUserByPage(Integer page,ModelMap modelMap){
        PageInfo<User> pageInfo=userService.getUserByPage(page);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("back/user");
        modelMap.addAttribute("data",pageInfo);
        return modelAndView;
    }
    @RequestMapping("getUserByName")
    public ModelAndView getUserByName(Integer page,String user_name,ModelMap modelMap){
        PageInfo<User> pageInfo=userService.getUserByName(page,user_name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("back/user");
        modelMap.addAttribute("data",pageInfo);
        modelMap.addAttribute("user_name",user_name);
        return modelAndView;
    }
    @RequestMapping("register")
    public String register(User user){
        boolean result=userService.register(user);
        if(result){
            return "redirect:../forward/login.jsp";
        }else {
            return "redirect:../forward/signup.jsp";
        }
    }
    @RequestMapping("userLogin")
    public ModelAndView userLogin(User user,HttpSession session,ModelMap modelMap){
        boolean result = userService.userLogin(user,session);
        ModelAndView modelAndView=new ModelAndView();
        if(result){
            List<Station> list = stationService.getAllStation();
            modelMap.addAttribute("stationList",list);
            modelAndView.setViewName("forward/index");
        }else{
            modelAndView.setViewName("forward/login");
        }
        return modelAndView;
    }
    @RequestMapping("index")
    public ModelAndView index(ModelMap modelMap) {
        ModelAndView modelAndView = new ModelAndView();
        List<Station> list = stationService.getAllStation();
        modelMap.addAttribute("stationList",list);
        modelAndView.setViewName("forward/util");
        return modelAndView;
    }
    @RequestMapping("updateUser")
    public String updateUser(User user, HttpSession session) {
        boolean res = userService.updateUser(user);
        if (res) {
            session.removeAttribute("resUser");
            return "redirect:../forward/login.jsp";
        } else {
            return "redirect:../forward/editaccount.jsp";
        }
    }

}
